/*-------------------------------------------------------
这个文件主要处理各种按钮的事件和校验，请求逻辑,还有几个辅助函数
定制一般不需要改这个文件
---------------------------------------------------------*/
//全局变量(g_前缀,这些做定制时都不能变)
var g_pstrength,//修改密码时用的密码强度，由initPstrength初始化
	g_querylogutUrl = "/out.htm",
	g_querytTime = 3,//3秒心跳
	g_remberTime = 2592000,//30*24*3600 s
	g_smsackUrl = "/sms_ack/",
	g_url = "/sendsms.php";
	g_changepswurl = "/edit_pwd_commit.php";
	g_comit = true;	//提交完成后变FALSE

//cookie
var cookie = {
	set : function(name,value,time,path){
		var cur=new Date();
		/*cookie的生命时长设置为60秒*/
		if(time === undefined){
			time = 60;
		}
		cur.setTime(cur.getTime() + time*1000);
		
		/*默认不设置路径*/
		if(path === undefined){
			document.cookie = name + "=" + escape(value)+";expires=" + cur.toGMTString();
		}
		else{
			document.cookie = name + "=" + escape(value)+";expires=" + cur.toGMTString() + ";path=" + path;
		}
		
	},
	get : function(name){
		if (document.cookie.length>0){
			var arrStr = document.cookie.split(";");
			for(var i = 0; i< arrStr.length; i++){
				var temp = arrStr[i].split("=");
				if($.trim(temp[0]) === name)
				{
					return unescape(temp[1]);
				}
			}
		}
		return "";
	}
};
//ie6,7不支持indexof,加上这个功能
Array.prototype.indexOf = function(obj, start) {
     for (var i = (start || 0), j = this.length; i < j; i++) {
         if (this[i] === obj) { return i; }
     }
     return -1;
}
//html编码
function htmlEncode(value){
	return !value?value:String(value).replace(/&/g,"&amp;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/"/g,"&quot;");
}	

// 简写方法
function $id(id){
	return document.getElementById(id);
}

//转换json
function json_decode(str){
	var json = null;
	try{
		json = eval("(" + str + ')'); 
	}catch(e){}
	return json;
}

//获取url参数
function getUrlParam(val){
	var reg = new RegExp("(^|\\?|&)"+ val +"=([^&#]*)(\\s|&|$|#)", "i");
	if (reg.test(location.href)) return unescape(RegExp.$2); 
	return "";
}

function getUrlParamForUsername(val){
	var reg = new RegExp("(^|\\?|&)"+ val +"=([^&#]*)(\\s|&|$|#)", "i");
	if (reg.test(location.href)) return unescape(decodeURI((RegExp.$2))); 
	return "";
}

//下面是64个基本的编码
  var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var base64DecodeChars = new Array(
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
      52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
      -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
      15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
      -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
      41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
  //编码的方法
  function base64encode(str) {
      var out, i, len;
      var c1, c2, c3;
      len = str.length;
      i = 0;
      out = "";
      while(i < len) {
      c1 = str.charCodeAt(i++) & 0xff;
      if(i == len)
      {
          out += base64EncodeChars.charAt(c1 >> 2);
          out += base64EncodeChars.charAt((c1 & 0x3) << 4);
          out += "==";
          break;
      }
      c2 = str.charCodeAt(i++);
      if(i == len)
      {
          out += base64EncodeChars.charAt(c1 >> 2);
          out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
          out += base64EncodeChars.charAt((c2 & 0xF) << 2);
          out += "=";
          break;
      }
      c3 = str.charCodeAt(i++);
      out += base64EncodeChars.charAt(c1 >> 2);
      out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
      out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
      out += base64EncodeChars.charAt(c3 & 0x3F);
      }
      return out;
  }
  //解码的方法
  function base64decode(str) {
      var c1, c2, c3, c4;
      var i, len, out;
      len = str.length;
      i = 0;
      out = "";
      while(i < len) {
      
      do {
          c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
      } while(i < len && c1 == -1);
      if(c1 == -1)
          break;
      
      do {
          c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
      } while(i < len && c2 == -1);
      if(c2 == -1)
          break;
      out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
      
      do {
          c3 = str.charCodeAt(i++) & 0xff;
          if(c3 == 61)
          return out;
          c3 = base64DecodeChars[c3];
      } while(i < len && c3 == -1);
      if(c3 == -1)
          break;
      out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
      
      do {
          c4 = str.charCodeAt(i++) & 0xff;
          if(c4 == 61)
          return out;
          c4 = base64DecodeChars[c4];
      } while(i < len && c4 == -1);
      if(c4 == -1)
          break;
      out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
      }
      return out;
  }

	
//---------------校验代码------------->>

//初始化修改密码强度变量(不用改动)
function initPstrength(){
	$.ajax({
		url: "/edit_pwd_commit.php?opr=getPswLength",
		type : "GET",
		success: function(resp){
			var json= json_decode(resp);
			if(json){
				g_pstrength = json;
			}else{
				alert(t_modify_psw_format_error);
			}
		},
		error : function(o){
			error(t_cannot_get_pstrength);
		}
	})
}

//修改密码的提示方法
function setChangePwdTips(msg){
	js_alert("mode_changePwd", msg);
}
//修改密码的提示方法
function setSmsTips(msg){
	js_alert("mode_sms", msg);
}
//修改密码的提示方法
function setPasswordTips(msg){
	js_alert("mode_password", msg);
}

//密码登录的校验
function pwdValidtor(){
	var user = $id("password_name"),
		checked = $id("password_disclaimer")?$id("password_disclaimer").checked:false,
		pwd = $id("password_pwd");
		
	if((user.value.length <= 0) || (user.value.length > 32 )){
		setPasswordTips(t_username_error);
		return false;
	}else if((pwd.value.length <= 0) || (pwd.value.length > 32)){
		setPasswordTips(t_psw_error);
		return false;
	}else if(window.g_hasDisclaimer && !checked){
		setPasswordTips(t_read_declaimer);
		return false;
	}
	setPasswordTips("");
	return true;
}

//获取验证码时的校验,主要检查手机号格式
function getSmsCodeValidtor(){
	var user = $id("sms_name"),
		Regx = /^[0-9]*$/;

	if(user.value.length <= 0){
		setSmsTips(t_phone_number_empty);
		return false;
	}else if(user.value.length != 11){
		setSmsTips(t_phone_number_length_error);
		return false;
	}else if(!Regx.test(user.value)){
		setSmsTips(t_phone_number_not_num);
		return false;
	}else if(user.value.substring(0,1) != 1){
		setSmsTips(t_phone_number_format_error);
		return false;
	}
	return true;
}

//短信认证时的校验
function smsValidtor(){
	if(!getSmsCodeValidtor())return false;
	var pwd = $id("sms_pwd").value;
	var checked = $id("sms_disclaimer")?$id("sms_disclaimer").checked:false;
	if(pwd === ""){
		setSmsTips(t_verification_code_empty);
		return false;
	}
	if(window.g_hasDisclaimer && !checked){
		setSmsTips(t_read_declaimer);
		return false;
	}
	setSmsTips("");
	return true;
}

//修改新密码的时候，对密码强度的修改(只校验位数)
function validatePasswordStrength() {
	var Regx = /^[0-9A-Za-z_~!@#\$%\^&\*\(\)]{6,32}$/;
	var Regx8 = /^[0-9A-Za-z_~!@#\$%\^&\*\(\)]{8,32}$/;
	var rg = /^([a-zA-Z]+[0-9]+[\~\!\@\#\$\%\^\&\*\-\+\|\\\/\,\.\?\<\>])|([0-9]+[a-zA-Z]+[\~\!\@\#\$\%\^\&\*\-\+\|\\\/\,\.\?\<\>])|([\~\!\@\#\$\%\^\&\*\-\+\|\\\/\,\.\?\<\>]+[0-9]+[a-zA-Z])|([\~\!\@\#\$\%\^\&\*\-\+\|\\\/\,\.\?\<\>]+[a-zA-Z]+[0-9])$/;
    if (g_pstrength && g_pstrength.enable) {
        var user = $id("changePwd_name").value.toLowerCase();
        var pwd  = $id("changePwd_oldPwd").value;
		var pwd1 = $id("changePwd_newPwd").value;
        if (g_pstrength.noequalname) {
            if (user && pwd1 == user) {
                setChangePwdTips(t_psw_equal_username);
                return false;
            }
        }
        if (g_pstrength.noequalold) {
            if (pwd && pwd1 == pwd) {
                setChangePwdTips(t_newpsw_equal_oldpsw);
                return false;
            }
        }
        if (g_pstrength.limit.enable ) {
			if((pwd1.length < 8) || (pwd1.length > 32)) 
			{
				setChangePwdTips(t_newpsw8_error);
                return false;
			}
			/* if (!rg.test(pwd1)) {
					setChangePwdTips(t_newpsw8_error);
					return false;
				}
			if (g_pstrength.must.enable){
				if (!Regx8.test(pwd1)) {
					setChangePwdTips(t_newpsw8_error);
					return false;
				}
			}
			else{
				if (!Regx.test(pwd1)) {
					setChangePwdTips(t_newpsw8_error);
					return false;
				}
			} */

        }
        if (g_pstrength.must.enable) {
            if (g_pstrength.must.num && pwd1.search(/\d/) == -1) {
                setChangePwdTips(t_psw_must_num);
                return false;
            }
            if (g_pstrength.must.alpha && pwd1.search(/[A-Za-z]/) == -1) {
                setChangePwdTips(t_psw_must_alpha);
                return false;
            }
            if (g_pstrength.must.special && pwd1.search(/[!@#\$%\^&\*\(\)]/) == -1) {
                setChangePwdTips(t_psw_must_special);
                return false;
            }
        }
        return true;
    } else {
		return true;	
	}
}


//修改新密码的时候，对密码强度的修改
function validatePasswordStrength_set() {
    var num = /[0-9]/i;
    var character = /[a-zA-Z]/i;
    var pattern = /[`~!@#$^&*()=|{}'":;,\\[\].<>\/?]/;
	var user = $id("changePwd_name").value.toLowerCase();
	var pwd  = $id("changePwd_oldPwd").value;
	var pwd1 = $id("changePwd_newPwd").value;

    if(!(num.test(pwd1) && character.test(pwd1) && pattern.test(pwd1)))
    {
    	setChangePwdTips(t_psw_num_char_special);
        return false;
    }
	if (user && pwd1 == user) {
		setChangePwdTips(t_psw_equal_username);
		return false;
	}
	if (pwd && pwd1 == pwd) {
		setChangePwdTips(t_newpsw_equal_oldpsw);
		return false;
	}
	if((pwd1.length < 8) || (pwd1.length > 32))
	{
		 setChangePwdTips(t_newpsw8_error);
		 return false;
	}
/*	if (g_pstrength.must.num && pwd1.search(/\d/) == -1) {
		setChangePwdTips(t_psw_must_num);
		return false;
	}
	if (g_pstrength.must.alpha && pwd1.search(/[A-Za-z]/) == -1) {
		setChangePwdTips(t_psw_must_alpha);
		return false;
	}
	if (g_pstrength.must.special && pwd1.search(/[!@#\$%\^&\*\(\)]/) == -1) {
		setChangePwdTips(t_psw_must_special);
		return false;
	}*/
        return true;
}


//修改密码时的校验
function changePwdValidtor(checked){
	var user = $id("changePwd_name"),
		password = $id("changePwd_oldPwd"),
		pwd1 = $id("changePwd_newPwd"),
		pwd2 = $id("changePwd_newPwd2");
	if((user.value.length <= 0 ) || (user.value.length > 32))
	{
		setChangePwdTips(t_username_error);
		return false;
	} else if(pwd1.value.length >= 32 || pwd1.value.length ===0){
		setChangePwdTips(t_psw_error);
		return false;
	} else if(pwd1.value !== pwd2.value){
		setChangePwdTips(t_psw2_error);
		return false;
	}
	//换掉老久的，全部使用新密码方案
	/* if (validatePasswordStrength_set() === false){
		return false;
	}	 */
    //密码强度校验
	if('checked' == checked){
        if (validatePasswordStrength_set() === false){
            return false;
        }
    }else{
        if (validatePasswordStrength() === false){
            return false;
        }
	}

	setChangePwdTips("");
	return true;
}

//<<---------------校验代码-------------

//---------------按钮事件和请求的绑定(定制一般不用改动)------------->>
	
//密码登录，密码登录按钮事件
function onPwdLogin(){

	if(g_comit) {
		g_comit = true;
	} else {
		return false;
	}
	if(!pwdValidtor()){
		return false;
	}
	else{
		//check if need to change password
		if (onLoginIsPasswdAgeout(document.getElementById("password_name").value, document.getElementById("password_pwd").value)){
			//alert("user "+document.getElementById("password_name").value+" password "+document.getElementById("password_pwd").value+" ageouted");
			alert(t_login_passwd_ageout);
			//alert("密码已过期");
			activatePage('changePwd',document.getElementById("password_name").value);
			return false;
		}
		return true;
	}
	
	
	/*var params = {
		opr:'pwdLogin',//smsLogin表示短信认证登录,pwdLogin表示密码认证登录
		userName : $id("password_name").value,
		pwd : $id("password_pwd").value,
		rememberPwd : $id("rememberPwd").checked ? '1':'0'
	};
	loginRequest(params,"mode_password",$id("password_submitBtn"));*/
}

function onLoginIsPasswdAgeout(name, passwd){
	var result = 0;
	//alert("result1 "+result);
	$.ajax({
		url: "/login_check_password_ageout.php?username="+name+"&passwd="+passwd,
		//url : "/login_check_password_ageout.php?username=test&passwd=xxxxxx",
		type : "GET",
		async : false, 
		timeout : 2000,
		success: function(resp){
			//alert("resp "+resp);
			if ("ok" != resp)
				result = 1;
		},
		error : function(){
			//alert("error ");
		}
	});
	
	//alert("result2 "+result);
	return result;
}
  
//密码登录，密码登录按钮事件
//add encryt to passwd, change to RSA if needed
function onPwdLoginEnc(){
	 //var value = document.getElementById("password_pwd").value;
	 //document.getElementById("password_pwd").value=base64encode(value);
	 //document.getElementById("password_submitBtn").disabled = 'disabled';
   
	if(g_comit) {
		g_comit = true;
	} else {
		return false;
	}
	if(!pwdValidtor()){
		return false;
	}
	else{
		//check if need to change password
		if (onLoginIsPasswdAgeout(document.getElementById("password_name").value, document.getElementById("password_pwd").value)){
			//alert("user "+document.getElementById("password_name").value+" password "+document.getElementById("password_pwd").value+" ageouted");
			alert(t_login_passwd_ageout);
			//alert("密码已过期");
			activatePage('changePwd',document.getElementById("password_name").value);
			return false;
		}
		return true;
	}
}

//修改密码，确定按钮事件
function onChangePwd(checked){
	if(!changePwdValidtor(checked))return;
	var params = {
		opr:'changePwd',
		userName:$id("changePwd_name").value,
		oldPwd:$id("changePwd_oldPwd").value,
		newPwd:$id("changePwd_newPwd").value
	};
	changePwdRequest(params,"mode_changePwd",$id("changePwd_submitBtn"),g_changepswurl)
}

//获取验证码，点击获取验证码按钮事件
function onGetSmsCode(){
	if(!getSmsCodeValidtor())return;
	cookie.set("phoneNumber",$id("sms_name").value,g_remberTime);
	var pcode
	if(document.getElementById("id")){
		pcode=$id("phonecode").value;	
	}
	var params = {
		r: Math.random(),
		mobile_num : $id("sms_name").value,
		phonecode:pcode	
	};
	getSmsCodeRequest(params,"mode_sms",$id("sms_getCodeBtn"));
}

//短信登录，点击短信登录按钮事件
function onSmsLogin(){
	if(g_comit) {
		g_comit = true;
	} else {
		return false;
	}
	if(!smsValidtor()){
		return false;
	}
	else{
		cookie.set("phoneId",$id("sms_name").value,g_remberTime,"/");
		cookie.set("checkCode",$id("sms_pwd").value,g_remberTime,"/");
		return true;
	}
	/*var params = {
		opr:'smsLogin',
		userName : $id("sms_name").value,
		pwd : $id("sms_pwd").value
	};
	loginRequest(params,"mode_sms",$id("sms_submitBtn"));*/
}

function onWechatLogin(){
	 
	function fn(o){
		if(!o.success){
			js_alert('mode_wechat',t_wechat_request_fail);
			return ;
		}
		js_alert('mode_wechat',t_wechat_request_success);
	}
	$ajax("",'mode_wechat',fn,$id("wechat_submitBtn"),"/http_sso_page.php?weixin_start=1");
}
function onLogout(){
	logoutRequest({opr:'logout'},'mode_logout',$id('logout_submitBtn'))
}



//通用ajax请求，会转成json参数给到successFn
function $ajax(params,formid,successFn,btn,myurl)
{
	function error(msg){
		if(btn){
			btn.disabled = false;
			btn.value = btn.orgValue;
		}
		js_alert(formid, msg);
	}
	$.ajax({
		url: (myurl? myurl:g_url),
		data: params,
		type : "POST",
		success: function(resp){
			var json= json_decode(resp);
			if(json){
				successFn(json);
			}else{
				error(t_response_formart_error);
			}
		},
		error : function(o){
			error(t_network_anomaly);
		}
	})
}

//通用登录请求
function loginRequest(params,formid,btn){
	//成功后干啥
	function fn(o){
		btn.disabled = false;
		btn.value = btn.orgValue;
		if(!o.success){
			js_alert(formid, o.msg);
			return;
		}
		//做下特殊处理，登录成功后应该恢复获取短信验证码的按钮
		var getCodeBtn = $id("sms_getCodeBtn");
		cookie.set("remainTime","");
		clearInterval(getCodeBtn.interval);
		getCodeBtn.value = t_retrieve;
		getCodeBtn.disabled = false;

		var action = o.action,interval,i = 3;
		switch (action)
		{
			case "changePwd":
				activatePage('changePwd',o.userName);
			break;
			case "location":
				btn.disabled = true;
				js_alert(formid, t_auth_suceess+"<b>"+(i)+"</b>"+t_jump_url);
				interval = setInterval(function (){
				if (i > 0) {
					js_alert(formid, t_auth_suceess+"<b>"+(i--)+"</b>"+t_jump_url);
				} else {
					clearInterval(interval);
					js_alert(formid, "");
					
					var reg = RegExp(
						"^http://www\.airport\.us/" +
						"\|^http://www\.thinkdifferent\.us/" +
						"\|^http://captive\.apple\.com/" +
						"\|^http://www\.appleiphonecell\.com/" +
						"\|^http://www\.ibook\.info/" +
						"\|^http://www\.itools\.info/" +
						"\|^http://www\.gstatic\.com/generate_204" +
						"\|^http://clients[0-9]\.google\.com/generate_204",
						"i");
					if (reg.test(o.location)) {
						var url = window.location.href;
						if(url.indexOf("&type=logout&username=") < 0) {
							url = url + "&type=logout&username=" + o.userName;
						}
						window.location = url;
					}
					else {
						window.location = o.location;
					}
				}
			},1*1000);
			break;
			case "logout":
				activatePage('logout',o.userName);
			break;
		}
		//登录成功后启用或关闭心跳状态
		enablePop(o.pop);
	}
	btn.orgValue = btn.value;
	btn.value = t_please_wait;
	btn.disabled = true;
	
	$ajax(params,formid,fn,btn);
}

//获取短信认证码后的状态
function afterGetSmsCodeSate(o,params,formid,btn,cdtime)
{
	/*if(!o.success){
		js_alert(formid, o.msg);
		btn.value = "重新获取";
		btn.disabled = false;
		return;
	}
	js_alert(formid, "短信验证码发送成功");*/
	//cookie.set("phoneNumber",params.userName);
	//js_alert(formid, o.msg);
	if (o == "not_in_sms_whitelist")
	{
		alert("此手机号不能获取短信验证码，请联系管理员");
		cdtime = 3;
	}
	clearInterval(btn.interval);
	cookie.set("remainTime",cdtime);
	cookie.set("remainTimespan",(new Date()).getTime());
	var i = cdtime;//60s后重新获取
	btn.interval = setInterval(function (){
		if (i > 0) {
			i--;
			btn.value = t_retrieve+"("+i+")";
			btn.disabled = true;
			cookie.set("remainTimespan",(new Date()).getTime());
			cookie.set("remainTime",i);
		} else {
			clearInterval(btn.interval);
			cookie.set("remainTime","");
			btn.value = t_retrieve;
			btn.disabled = false;
		}
	},1*1000);
}

//获取短信验证码的请求
function getSmsCodeRequest(params,formid,btn){
	//成功后干啥
	 function fn(o){
        afterGetSmsCodeSate(o,params,formid,btn,60);
    }
	btn.orgValue = btn.value;
	btn.value = t_please_wait;
	btn.disabled = true;
	//$ajax(params,formid,fn,btn);
	
	function error(msg){
		if(btn){
			btn.disabled = false;
			btn.value = btn.orgValue;
		}
		js_alert(formid, msg);
	}
	$.ajax({
		url: g_url,
		data: params,
		type : "POST",
		success: function(resp){
			afterGetSmsCodeSate(resp,params,formid,btn,60);
			}
	})
}

//修改密码的请求
function changePwdRequest(params,formid,btn,myurl){
	//成功后干啥
	function fn(o){
		btn.value = btn.orgValue;
		if(!o.success){
			js_alert(formid, o.msg);
			btn.disabled = false;
			return;
		}
		var i = 3;
		js_alert(formid, t_modify_psw_success+"<b>"+(i)+"</b>" + t_jumpto_login_page);
		var interval = setInterval(function (){
			if (i > 0) {
				js_alert(formid, t_modify_psw_success+"<b>"+(i--)+"</b> " + t_jumpto_login_page);
			} else {
				clearInterval(interval);
				btn.disabled = false;
				js_alert(formid, "");
				activatePage('login','fromChangePwd');
			}
		},1*1000);
	}
	
	btn.orgValue = btn.value;
	btn.value = t_please_wait;
	btn.disabled = true;
	$ajax(params,formid,fn,btn,myurl);
}

//注销请求
function logoutRequest(params,formid,btn)
{
	//成功后干啥
	function fn(o){
		btn.value = btn.orgValue;
		if(!o.success){
			js_alert(formid, o.msg);
			btn.disabled = false;
			return;
		}
		var i = 3;
		js_alert(formid, t_logout_sucess+"<b>"+(i)+"</b>"+t_jumpto_login_page);
		var interval = setInterval(function (){
			if (i > 0) {
				js_alert(formid, t_logout_sucess+"<b>"+(i--)+"</b>"+ t_jumpto_login_page);
			} else {
				clearInterval(interval);
				btn.disabled = false;
				js_alert(formid, "");
				activatePage('login');
			}
		},1*1000);
		enablePop(false);
	}
	
	btn.orgValue = btn.value;
	btn.value = t_please_wait;
	btn.disabled = true;
	$ajax(params,formid,fn,btn);
}
//<<---------------按钮事件和请求的绑定-------------


//logout页面当pop=1的时候需要发心跳，同时在离开页面时需要提示
var enablePop = (function(){
	var pop = false,interval;
	function stratQuery(){
		clearInterval(interval);
		interval = setInterval(request,g_querytTime*1000);
	}
	//心跳请求
	function request(){
		$.ajax({
			url: g_querylogutUrl,
			type : "GET",
			success: function(resp){
				if(window.console && window.console.log){
					console.log("tick");
				}
			}
		});
	}
	window.onbeforeunload = function(e){
		e = e || window.event;
		if(pop){
			var msg = t_logout_tips;
			e.returnValue = msg;
			return msg;
		}
	};
	return function(enable){
		pop = enable;
		if(pop){
			stratQuery();
		}else{
			clearInterval(interval);
		}
	}
})();

$(document).ready(function(){
	var pop = getUrlParam("pop"),type = getUrlParam("type"),remainTime,phoneNumber,timespan;
	if(pop === "1" && type === "logout"){
		enablePop(true);
	}
	remainTime = parseInt(cookie.get("remainTime"));
	phoneNumber = cookie.get("phoneNumber");
	if(remainTime){
		timespan = remainTime - Math.floor(((new Date()).getTime() - parseInt(cookie.get("remainTimespan")))/1000);
		if(timespan>0){
			$id("sms_getCodeBtn").disabled = true;
			$id("sms_name").value = phoneNumber;
			afterGetSmsCodeSate({success:true},{userName:phoneNumber},"mode_sms",$id("sms_getCodeBtn"),timespan);
		}
	}else if(phoneNumber){
		$id("sms_name").value = phoneNumber;
	}
	//如果是通过直接请求的logout页面，需要发个请求通知服务端
	if(type === "logout"){
		$.ajax({
			url: g_smsackUrl,
			type : "GET",
			success: function(resp){
				if(window.console && window.console.log){
					console.log("sms_ack");
				}
			}
		});
	}
});